const jwt = require('jsonwebtoken');
const AUTH_KEY = 'evst-platform-token'
export default {
  state: {
    userInfo: {
      id: "",
      userName: "",
      userDesc: "",
      avatarPath: "",
      userDesc: "",
      phone: "",
      address: "",
      email: "",
      enabled: "",
    }, //当前登录用户的个人信息
    perms: [], // 当前登录用户的权限集合
    token: "",
    checkToken: false,
    jwtObj: { //当前登录用户的用户令牌包含的信息
      sub: "", //用户名
      iat: "", //开始时间
      exp: "", //超时时间
    },
  },
  getters: {
    checkToken: (state) => () => {
      let token = (status.token === "" || null == status.token) ? window.sessionStorage.getItem(AUTH_KEY) : state.token;
      return (token) ? true : false;
    },
    token: (state) => () => {
      let token = (status.token === "" || null == status.token) ? window.sessionStorage.getItem(AUTH_KEY) : state.token;
      return "xytoken_" + token;
    },
    userInfo(state) {
      let userName = state.userInfo.userName;
      if (null == userName || "" === userName) {
        let token = (status.token === "" || null == status.token) ? window.sessionStorage.getItem(AUTH_KEY) : state.token;
        if (token && token !== "undefined") {
          state.jwtObj = jwt.decode(token);
          state.userInfo.userName = state.jwtObj.sub;
        }
      }
      return state.userInfo;
    }
  },
  mutations: {
    getUserInfo(state) {
      return state.userInfo
    },
    getToken(state) {
      return state.token;
    },

    setPerms(state, perms) { // 用户权限标识集合
      state.userInfo.perms = perms;
    },
    setToken(state, token) { //当前用户的token令牌
      window.sessionStorage.setItem(AUTH_KEY, token)
      state.token = token;
      state.jwtObj = jwt.decode(token);
      state.userInfo.userName = state.jwtObj.sub; //更新当前用户的用户名
    },
    delToken(state) { //清除用户登录令牌
      state.token = "";
      state.userInfo = {}; //清除当前用户的个人信息
      state.checkToken = false;
      window.sessionStorage.removeItem(AUTH_KEY);
    },
    setUserInfo(state, userInfo) { //设置当前用户信息
      state.userInfo = userInfo
    },
  },
  actions: {}
}
